﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Messaging.Design;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BLL;
using Sunny.UI;

namespace socketForm
{
    public partial class FrmConfig : UIForm
    {
        public FrmConfig()
        {
            InitializeComponent();
        }

        private void but_start_Click(object sender, EventArgs e)
        {
            var cbbSqlType = this.cbbSqlType.Text;
            var txtSqlAddress = this.txtSqlAddress.Text;
            var txtSqlSort = this.txtSqlSort.Text;
            var txtSqlSelect = this.txtSqlSelect.Text;

            SqlConfig.sqlType = cbbSqlType;
            SqlConfig.sqlAddress = txtSqlAddress;
            SqlConfig.sqlSelcet = txtSqlSelect;
            SqlConfig.sqlSqlSort = txtSqlSort;

            string configinfo = string.Format("数据库类型:{0}\r\n数据库链接地址:{1}\r\n数据库排序字段:{2}", cbbSqlType, txtSqlAddress, txtSqlSort);
            WriteLogPublic.WriteLogFunStr(configinfo);
            DialogResult dialogResult = MessageBox.Show(configinfo, "确认配置", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (dialogResult == DialogResult.OK)
            {
                if (String.IsNullOrEmpty(txtSqlSort.Trim()))
                {
                    MessageBox.Show("数据库排序字段", "数据输入有误", MessageBoxButtons.OK);
                }
                else {
                    this.saveIni();
                    // 隐藏当前窗体
                    this.Hide();
                    FrmServer frmServer = new FrmServer(this);
                    frmServer.ShowDialog();
                }
            }
        }
        private void saveIni() {
            // 保存到配置文件
            IniFileHelper.Instance.WriteIniString(SqlConfig.sqlConfigSection, SqlConfig.sqlConfigType, SqlConfig.sqlType);
            IniFileHelper.Instance.WriteIniString(SqlConfig.sqlConfigSection, SqlConfig.sqlConfigAddress, SqlConfig.sqlAddress);
            IniFileHelper.Instance.WriteIniString(SqlConfig.sqlConfigSection, SqlConfig.sqlConfigSelect, SqlConfig.sqlSelcet);
            IniFileHelper.Instance.WriteIniString(SqlConfig.sqlConfigSection, SqlConfig.sqlConfigSqlSort, SqlConfig.sqlSqlSort);
        }
        private void readConfig()
        {
            // 读取配置文件
            var sqlConfigType = IniFileHelper.Instance.GetIniString(SqlConfig.sqlConfigSection, SqlConfig.sqlConfigType);
            if (!String.IsNullOrEmpty(sqlConfigType)) this.cbbSqlType.Text = sqlConfigType;
            var sqlConfigAddress = IniFileHelper.Instance.GetIniString(SqlConfig.sqlConfigSection, SqlConfig.sqlConfigAddress);
            if (!String.IsNullOrEmpty(sqlConfigAddress)) this.txtSqlAddress.Text = sqlConfigAddress;
            var sqlConfigSelect = IniFileHelper.Instance.GetIniString(SqlConfig.sqlConfigSection, SqlConfig.sqlConfigSelect);
            if (!String.IsNullOrEmpty(sqlConfigSelect)) this.txtSqlSelect.Text = sqlConfigSelect;
            var sqlConfigSqlSort = IniFileHelper.Instance.GetIniString(SqlConfig.sqlConfigSection, SqlConfig.sqlConfigSqlSort);
            if (!String.IsNullOrEmpty(sqlConfigSqlSort)) this.txtSqlSort.Text = sqlConfigSqlSort;
        }
        private void cbbSqlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sqlAddress = "";
            var cbbSqlType = (UIComboBox) sender;
            switch (cbbSqlType.SelectedText) {
                case "SQL Server":
                    sqlAddress = SqlConfig.sqlServiceSql;
                    break;
                case "SqlLit":
                    sqlAddress = SqlConfig.sqlLitSql;
                    break;
                case "MySql":
                    sqlAddress = SqlConfig.mySqlSql;
                    break;
                case "Oracle":
                    sqlAddress = SqlConfig.oracleSql;
                    break;
                case "Access":
                    sqlAddress = SqlConfig.accessSql;
                    break;
                default:
                    sqlAddress = SqlConfig.accessSql;
                    break;
            }
            this.txtSqlAddress.Text = sqlAddress;
        }

        private void FrmConfig_Load(object sender, EventArgs e)
        {
            // 创建配置文件
            createConfigIni();
            // 读取配置
            this.readConfig();
        }
        private static void createConfigIni()
        {
            // ini配置文件路径
            string strIniFilePath = Directory.GetCurrentDirectory() + @"\Config\config.ini";
            // 创建配置文件
            string sfilepath = Directory.GetCurrentDirectory() + @"\Config"; // 根据项目名称等创建文件夹
            Directory.CreateDirectory(sfilepath); // 不存在则创建
            if (!File.Exists(strIniFilePath))
            {
                // 创建后释放资源，不然不能写入
                File.Create(strIniFilePath).Dispose();
            }
        }

        private void but_send_test_Click(object sender, EventArgs e)
        {
            TradSend tradSend = new TradSend(this.cbbSqlType.Text, this.txtSqlAddress.Text, this.txtSqlSelect.Text, this.txtSqlSort.Text, SqlConfig.dataSize);
            var sendStr = tradSend.findSendData();
            MessageBox.Show($"获取到的数据：【{sendStr}】");
        }
    }
}
